<?php 

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.


class PasstrongComponent extends Object {

	/**
	 * Comprueba la fuerza de una contraseña
	 *
	 * @param string $password cadena que se le pasa para comprobar su "fuerza"
	 * @return array $result_pass array con los resultados de la verificación
	 */
	function CheckPasswordStrength($password){

		$strength = 0;
		$patterns = array('#[a-z]#','#[A-Z]#','#[0-9]#','/[¬!"£$%^&*()`{}\[\]~;\'#<>?,.\/\\-=_+\|]/');
		/*Tipos de caracteres en la contraseña*/
		foreach($patterns as $pattern){
			if(preg_match($pattern,$password,$matches)){
				$strength++;
			}
		}
		
		/*Tamaño de la contraseña*/
		$tamanios = array('6','10','12','16');
		$tam_password = strlen($password);

		foreach($tamanios as $tamanio){
			if(strlen($password) > $tamanio){
				$strength++;
			}
		}
		
		/*Mensaje de fuerza*/
		switch ($strength){
		
			case 0:
			  $result_pass['strength_msg'] = I18n::translate("no_security");
			break;
			
			case 1:
			case 2:
			  $result_pass['strength_msg'] = I18n::translate("wake");
			break;

			case 3:
			case 4:
			  $result_pass['strength_msg'] = I18n::translate("good");
			break;

			case 5:
			case 6:
			  $result_pass['strength_msg'] = I18n::translate("strong");
			break;

			case 7:
			case 8:
			  $result_pass['strength_msg'] = I18n::translate("very_strong");
			break;

			default:
				$result_pass['strength_msg'] = I18n::translate("very_strong");
		
		}
		$result_pass['strength'] = $strength;
		
		return $result_pass;
	}
	/**
	 * Genera contraseñas de modo aleatorio
	 *
	 * @return string $cad contraseña generada
	 */
	function MakePassword(){
		$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
		$cad = "";
		for($i=0;$i<8;$i++) {
			$cad .= substr($str,rand(0,62),1);
		}
		return $cad;
	}


}
?>
